package login;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import smecta.client.print.domain.PTInputContext;
import smecta.client.print.domain.print.PrintCodeListEntity;
import smecta.client.print.mapper.SysUserMapper;
import smecta.client.print.service.print.PrintService;
import smecta.client.print.util.AlertUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LoginController {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PrintService printService;

    @FXML
    private TextField usernameField;
    @FXML
    private TextField passwordField;

    @FXML
    private void handleLogin() {
        String username = usernameField.getText();
        String password = passwordField.getText();

        if (true || authenticate(username, password)) {
            Stage stage = (Stage) usernameField.getScene().getWindow();
            stage.close();
            loadMainView();
        } else {
            AlertUtil.error("用户名或者密码错误!");
        }
    }

    @FXML
    private void handleCancel() {
        Stage stage = (Stage) usernameField.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void handleTest() {
//        PTInputContext context = new PTInputContext();
//        Map<String, Object> data = new HashMap<>();
//        data.put("pinming","涤纶低弹丝");
//        context.setPrinterName("Microsoft Print to PDF");
//        context.setTemplatePath("C:\\Users\\reflection\\Desktop\\文件归档\\箱单通用打印模板.xlsx");
//        context.setTemplateData(data);
//        printService.print(context);

        PTInputContext context = new PTInputContext();
        Map<String, Object> data = new HashMap<>();
        data.put("danhao","20240703131000001");
        data.put("pinming","涤纶低弹丝");
        data.put("guige","75D/48F-S");
        data.put("seze","白色");
        data.put("pihao","123");
        data.put("riqi","20240703");
        data.put("dengji","AA");

        List<PrintCodeListEntity> items = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            items.add(new PrintCodeListEntity(String.valueOf(i),"D1","6","0.00","-2.28"));
        }

        data.put("items", items);
        context.setPrinterName("Microsoft Print to PDF");
        context.setTemplatePath("C:\\Users\\reflection\\Desktop\\文件归档\\码单通用打印模板.xlsx");
        context.setTemplateData(data);
        printService.print(context);
    }


    private boolean authenticate(String username, String password) {
//        SysUser sysUser = new SysUser();
//        sysUser.setUserName(username);
//        sysUser.setPassword(password);
//        List<SysUser> users = sysUserMapper.selectSysUserList(sysUser);
//        if(CollectionUtils.isEmpty(users)){
//            return false;
//        }
        return true;
    }

    private void loadMainView() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/MainView.fxml"));
            Stage stage = new Stage();
            Scene scene = new Scene(loader.load());
            stage.setScene(scene);
            stage.setTitle("亿维通包装线单机管理软件˾");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
